package com.plugin.buildsrc.jiagu

import com.android.build.gradle.api.BaseVariant
import org.apache.commons.io.FileUtils
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.TaskAction

/**
 * 加固包任务
 * 职责:
 * @author Damon
 */
class ApkPackingTask extends DefaultTask {
    @Input
    BaseVariant mVariant
    @InputFile
    File outputDir

    ApkPackingTask() {
        group "packingApk"
    }

    @TaskAction
    void packing() {
        FileUtils.deleteDirectory(outputDir)
        outputDir.mkdirs()
        copyTo(mVariant.outputs.first().outputFile, new File(outputDir.getPath(), mVariant.outputs.first().outputFile.name))
        //EncMain.main(["itman@valorosoltd.com", mVariant.outputs.first().outputFile.getPath(), outputDir.getPath(), "", "458"])
        if (outputDir.listFiles() == null || outputDir.listFiles().length == 0 || !outputDir.listFiles().first().getName().contains(".apk")) throw new GradleException("Packing Fail")

    }

    void copyTo(File src, File dest) {
        def input = src.newInputStream()
        def output = dest.newOutputStream()
        output << input
        input.close()
        output.close()
    }
}